package com.school.aftersalemanagement.controller.ui.maintenanceremind;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.school.aftersalemanagement.R;
import com.school.aftersalemanagement.controller.adapter.BicycleManagementAdapter;
import com.school.aftersalemanagement.controller.ui.register.RegisterActivity;
import com.school.aftersalemanagement.entity.Bike;
import com.school.aftersalemanagement.global.BaseActivity;
import com.school.aftersalemanagement.global.BaseToolBarActivity;
import com.school.aftersalemanagement.service.BicycleService;
import com.school.aftersalemanagement.service.impl.BicycleServiceimpl;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnItemClick;

public class MaintenanceRemindActivity extends BaseToolBarActivity {

    @BindView(R.id.maintenanceRemindList)
    ListView maintenanceRemindList;
    BicycleManagementAdapter bicycleManagementAdapter;
    private BicycleService bicycleService;
    private List<Bike> bikes;
    private int GET_BIKELIST = 1;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == GET_BIKELIST) {
                List<Bike> bike = (List<Bike>)msg.obj;
                bikes.addAll(bike);
                if(!bikes.isEmpty())
                if(bikes.size()>=1) {
                    bicycleManagementAdapter.notifyDataSetChanged();
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintenanceremind);
        setToolbarText("保养提醒");
        bikes = new ArrayList<>();
        bicycleManagementAdapter = new BicycleManagementAdapter(this,bikes);
        maintenanceRemindList.setAdapter(bicycleManagementAdapter);
        bicycleService = new BicycleServiceimpl(this,mHandler);
        bicycleService.repairList(GET_BIKELIST);
    }

    @OnItemClick(R.id.maintenanceRemindList)
    public void onItemClicked(AdapterView<?> parent, View view, int position, long id){
                switch (parent.getId()){
                    case R.id.maintenanceRemindList:
                        Intent resultIntent = new Intent();
                        Bike bike = bikes.get(position);
                        resultIntent.putExtra("bikeNum",bike.getNumber());
                        setResult(RESULT_OK, resultIntent);
                        finish();
                        break;
                }
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
}
